home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / MimeWriter.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  7KB  |  196 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Generic MIME writer.
  5.  
  6. This module defines the class MimeWriter.  The MimeWriter class implements
  7. a basic formatter for creating MIME multi-part files.  It doesn't seek around
  8. the output file nor does it use large amounts of buffer space. You must write
  9. the parts out in the order that they should occur in the final file.
  10. MimeWriter does buffer the headers you add, allowing you to rearrange their
  11. order.
  12.  
  13. """
  14. import mimetools
  15. __all__ = [
  16.     'MimeWriter']
  17. import warnings
  18. warnings.warn('the MimeWriter module is deprecated; use the email package instead', DeprecationWarning, 2)
  19.  
  20. class MimeWriter:
  21.     """Generic MIME writer.
  22.  
  23.     Methods:
  24.  
  25.     __init__()
  26.     addheader()
  27.     flushheaders()
  28.     startbody()
  29.     startmultipartbody()
  30.     nextpart()
  31.     lastpart()
  32.  
  33.     A MIME writer is much more primitive than a MIME parser.  It
  34.     doesn't seek around on the output file, and it doesn't use large
  35.     amounts of buffer space, so you have to write the parts in the
  36.     order they should occur on the output file.  It does buffer the
  37.     headers you add, allowing you to rearrange their order.
  38.  
  39.     General usage is:
  40.  
  41.     f = <open the output file>
  42.     w = MimeWriter(f)
  43.     ...call w.addheader(key, value) 0 or more times...
  44.  
  45.     followed by either:
  46.  
  47.     f = w.startbody(content_type)
  48.     ...call f.write(data) for body data...
  49.  
  50.     or:
  51.  
  52.     w.startmultipartbody(subtype)
  53.     for each part:
  54.         subwriter = w.nextpart()
  55.         ...use the subwriter's methods to create the subpart...
  56.     w.lastpart()
  57.  
  58.     The subwriter is another MimeWriter instance, and should be
  59.     treated in the same way as the toplevel MimeWriter.  This way,
  60.     writing recursive body parts is easy.
  61.  
  62.     Warning: don't forget to call lastpart()!
  63.  
  64.     XXX There should be more state so calls made in the wrong order
  65.     are detected.
  66.  
  67.     Some special cases:
  68.  
  69.     - startbody() just returns the file passed to the constructor;
  70.       but don't use this knowledge, as it may be changed.
  71.  
  72.     - startmultipartbody() actually returns a file as well;
  73.       this can be used to write the initial 'if you can read this your
  74.       mailer is not MIME-aware' message.
  75.  
  76.     - If you call flushheaders(), the headers accumulated so far are
  77.       written out (and forgotten); this is useful if you don't need a
  78.       body part at all, e.g. for a subpart of type message/rfc822
  79.       that's (mis)used to store some header-like information.
  80.  
  81.     - Passing a keyword argument 'prefix=<flag>' to addheader(),
  82.       start*body() affects where the header is inserted; 0 means
  83.       append at the end, 1 means insert at the start; default is
  84.       append for addheader(), but insert for start*body(), which use
  85.       it to determine where the Content-Type header goes.
  86.  
  87.     """
  88.     
  89.     def __init__(self, fp):
  90.         self._fp = fp
  91.         self._headers = []
  92.  
  93.     
  94.     def addheader(self, key, value, prefix = 0):
  95.         '''Add a header line to the MIME message.
  96.  
  97.         The key is the name of the header, where the value obviously provides
  98.         the value of the header. The optional argument prefix determines
  99.         where the header is inserted; 0 means append at the end, 1 means
  100.         insert at the start. The default is to append.
  101.  
  102.         '''
  103.         lines = value.split('\n')
  104.         while lines and not lines[-1]:
  105.             del lines[-1]
  106.         while lines and not lines[0]:
  107.             del lines[0]
  108.         for i in range(1, len(lines)):
  109.             lines[i] = '    ' + lines[i].strip()
  110.         
  111.         value = '\n'.join(lines) + '\n'
  112.         line = key + ': ' + value
  113.         if prefix:
  114.             self._headers.insert(0, line)
  115.         else:
  116.             self._headers.append(line)
  117.  
  118.     
  119.     def flushheaders(self):
  120.         """Writes out and forgets all headers accumulated so far.
  121.  
  122.         This is useful if you don't need a body part at all; for example,
  123.         for a subpart of type message/rfc822 that's (mis)used to store some
  124.         header-like information.
  125.  
  126.         """
  127.         self._fp.writelines(self._headers)
  128.         self._headers = []
  129.  
  130.     
  131.     def startbody(self, ctype, plist = [], prefix = 1):
  132.         '''Returns a file-like object for writing the body of the message.
  133.  
  134.         The content-type is set to the provided ctype, and the optional
  135.         parameter, plist, provides additional parameters for the
  136.         content-type declaration.  The optional argument prefix determines
  137.         where the header is inserted; 0 means append at the end, 1 means
  138.         insert at the start. The default is to insert at the start.
  139.  
  140.         '''
  141.         for name, value in plist:
  142.             ctype = ctype + ';\n %s="%s"' % (name, value)
  143.         
  144.         self.addheader('Content-Type', ctype, prefix = prefix)
  145.         self.flushheaders()
  146.         self._fp.write('\n')
  147.         return self._fp
  148.  
  149.     
  150.     def startmultipartbody(self, subtype, boundary = None, plist = [], prefix = 1):
  151.         '''Returns a file-like object for writing the body of the message.
  152.  
  153.         Additionally, this method initializes the multi-part code, where the
  154.         subtype parameter provides the multipart subtype, the boundary
  155.         parameter may provide a user-defined boundary specification, and the
  156.         plist parameter provides optional parameters for the subtype.  The
  157.         optional argument, prefix, determines where the header is inserted;
  158.         0 means append at the end, 1 means insert at the start. The default
  159.         is to insert at the start.  Subparts should be created using the
  160.         nextpart() method.
  161.  
  162.         '''
  163.         if not boundary:
  164.             pass
  165.         self._boundary = mimetools.choose_boundary()
  166.         return self.startbody('multipart/' + subtype, [
  167.             ('boundary', self._boundary)] + plist, prefix = prefix)
  168.  
  169.     
  170.     def nextpart(self):
  171.         '''Returns a new instance of MimeWriter which represents an
  172.         individual part in a multipart message.
  173.  
  174.         This may be used to write the part as well as used for creating
  175.         recursively complex multipart messages. The message must first be
  176.         initialized with the startmultipartbody() method before using the
  177.         nextpart() method.
  178.  
  179.         '''
  180.         self._fp.write('\n--' + self._boundary + '\n')
  181.         return self.__class__(self._fp)
  182.  
  183.     
  184.     def lastpart(self):
  185.         '''This is used to designate the last part of a multipart message.
  186.  
  187.         It should always be used when writing multipart messages.
  188.  
  189.         '''
  190.         self._fp.write('\n--' + self._boundary + '--\n')
  191.  
  192.  
  193. if __name__ == '__main__':
  194.     import test.test_MimeWriter as test
  195.  
  196.